package org.example;

import com.google.gson.reflect.TypeToken;
import org.junit.Test;
import org.my.stock.model.MStockType;
import org.my.stock.util.MyUtil;

import java.beans.PropertyVetoException;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class stock_type_test {
    @Test
    public void shouldAnswerWithTrue()
    {
        String filePath = "/Users/chenfei/Documents/Python/DawnSqlClient/DawnSqlClient/static/stock/dong_fang_type.json";
        StringBuilder content = new StringBuilder();

        try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
            List<MStockType> lst = MyUtil.getGson().fromJson(String.valueOf(content), new TypeToken<List<MStockType>>() {}.getType());

            Connection conn = MyUtil.getConnection();

            for (MStockType m : lst) {
                PreparedStatement st = conn.prepareStatement("insert into stock_type (code, name, pinyin) values (?, ?, ?)");
                st.setString(1, m.getCode());
                st.setString(2, m.getName());
                st.setString(3, m.getPinyin());
                st.executeUpdate();

                st.close();
                System.out.println(m.toString());
            }
            conn.close();
            //System.out.println(content);
        } catch (IOException e) {
            System.err.println("读取文件失败: " + e.getMessage());
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
